<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/3/8
  Time: 18:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="../../../base.jsp" %>
<div class="modal-header">
    <h3 class="text-left text-primary">
        加速卡列表
        <button type="button" class="btn btn-primary" disabled=false
                onclick="">新增加速卡
        </button>
    </h3>
</div>
<form id="editCDcardForm">
    <table class="table table-hover">
        <thead>
        <tr>
            <th>#</th>
            <th>加速时间</th>
            <th>描述</th>
            <th>图片</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${cdCardList}" var="cdCard" varStatus="vs">

            <%--<input value="${cdCard.coolDownCardId}" class="hidden" name="cdCard.coolDownCardId">--%>
            <tr class="
                <c:if test="${cdCard.status == 50}">
                    success
                </c:if>
                <c:if test="${cdCard.status == 0}">
                    danger
                </c:if>
                ">
                <th scope="row">${vs.count}</th>
                <td class="edit ${cdCard.coolDownCardId}" name='cdCard.hotValue' span="毫秒" type="number">
                        ${cdCard.hotValue}毫秒
                </td>
                <td class="edit ${cdCard.coolDownCardId}" name='cdCard.descr' span="" type="text">
                        ${cdCard.descr}
                </td>
                <td>
                    <c:if test="${empty cdCardFiles[vs.count-1].fileId }">
                        <button type="button" class="btn btn-primary"
                                onclick="cdcard.uploadCardImg(null,'${cdCard.coolDownCardId}', ModuleCode.BG_IMG)">
                            新增图片
                        </button>
                    </c:if>
                    <c:if test="${!empty cdCardFiles[vs.count-1].fileId }">
                        <button type="button" class="btn btn-primary"
                                onclick="cdcard.uploadCardImg('${cdCardFiles[vs.count-1].fileId}','${cdCard.coolDownCardId}', ModuleCode.BG_IMG)">
                            替换图片
                        </button><br>
                        <img src="${fileBaseUrl}/sysfile/download?sysFile.fileId=${cdCardFiles[vs.count-1].fileId}"
                             alt=" 加载失败" class="img-thumbnail">
                    </c:if>
                </td>
                <td>
                    <c:if test="${cdCard.status == 0}">
                        <strong style="color: black">[未发布]</strong>
                    </c:if>
                    <c:if test="${cdCard.status == 50}">
                        <strong style="color: green">[已发布]</strong>
                    </c:if>
                    <c:if test="${cdCard.status == 99}">
                        <strong style="color: red">[已废除]</strong>
                    </c:if>
                </td>
                <td>
                    <button type="button" class="btn btn-primary editBtn edit" cdCardId='${cdCard.coolDownCardId}'
                            onclick="cdcard.toEdit('${cdCard.coolDownCardId}',this)">修改
                    </button>
                    <button type="button" class="btn btn-danger saveBtn hidden edit submit"
                            cdCardId='${cdCard.coolDownCardId}'
                            onclick="cdcard.save('${cdCard.coolDownCardId}')">提交
                    </button>
                    <button type="button" class="btn btn-primary saveBtn hidden cancel edit"
                            cdCardId='${cdCard.coolDownCardId}'
                            onclick="cdcard.cancel('${cdCard.coolDownCardId}')">取消
                    </button>
                </td>

            </tr>

        </c:forEach>
        </tbody>
    </table>
</form>
<script>
    var cdcard = {
        uploadCardImg: function (preFileId, relationId, moduleCode) {
            confirm("上传新的图片会自动删除旧的图片，是否继续？", function () {
                JWB.http.uploadImgPage(relationId, moduleCode, function (res) {
                    if (res.code == 0) {
                        alert(res.msg);
                        if (preFileId != null && preFileId != "") {
                            var url = base.basePath + "sysfile/delete";
                            var data = {
                                "sysFile.fileId": preFileId
                            };
                            $.ajax({
                                url: url,
                                data: data,
                                success: function (res) {
                                    if (res.code != 0) {
                                        alert("删除旧的图片失败，为了避免出问题，请联系技术人员");
                                    } else {
                                        index_js.reloadRightPage();
                                    }
                                },
                                error: function (jXHR, textStatus, errorThrown) {
                                    JWB.http.errorHandle(jXHR, textStatus, errorThrown)
                                }
                            });
                        } else {
                            index_js.reloadRightPage();
                        }
                    }
                    $("#uploadFloatageWindow").remove();
                });
            });
        },
        cancel: function (cdCardId) {
            var elms = $("[editInput=1]");
            $(elms).each(function (i, o) {
                var old = $(o).attr("old");
                var td = $(o).parent();
                td.html(old + td.attr("span"));
            });

            $(".edit").removeClass("hidden");
            $(".submit").addClass("hidden");
            $(".cancel").addClass("hidden");
            $("button.edit").removeAttr("disabled")
        },
        toEdit: function (cdCardId, elem) {
            $('.edit.' + cdCardId).each(function () {
                var old = $(this).html();
                var name = $(this).attr("name");
                var span = $(this).attr("span");
                old = old.replace(span, "");
                var type = $(this).attr("type")
                var html = "<div class=\"input-group\" editInput='1' old='" + old + "'>\n" +
                    "    <input name='" + name + "' type=\"" + type + "\" class=\"form-control\"  aria-describedby=\"basic-addon2\" value=" + old + ">\n" +
                    "    <span class=\"input-group-addon\" id=\"basic-addon2\">" + span + "</span>\n" +
                    "</div>"
                $(this).html(html);

            });


            $("button.edit").attr("disabled", "");
            $("[cdCardId=" + cdCardId + "]").removeClass("hidden").removeAttr("disabled");
            $(".cancel").removeAttr("disabled");
            $(elem).addClass("hidden");
        },
        save: function (cdCardId) {

            var form = $("#editCDcardForm");
            var data = JWB.utils.fromSerialize(form);
            data["cdCard.coolDownCardId"] = cdCardId;
            var url = base.basePath + "cdcard/save";
            $.ajax(
                {
                    url: url,
                    data: data,
                    type: "POST",
                    success: function (res) {
                        alert(res.msg);
                        if (res.code == 0) {
                            index_js.reloadRightPage();
                        }
                    },
                    error: function (jXHR, textStatus, errorThrown) {
                        JWB.http.errorHandle(jXHR, textStatus, errorThrown);
                    }
                }
            );
        }
    }
</script>